package android.medicalcloud.com.yiapp.yilian.ui;

import android.content.IntentFilter;
import android.medicalcloud.com.yiapp.R;
import android.medicalcloud.com.yiapp.base.activity.BaseActivity;
import android.medicalcloud.com.yiapp.base.cach.PreferceManager;
import android.medicalcloud.com.yiapp.base.net.lisener.ViewNetCallBack;
import android.medicalcloud.com.yiapp.base.utils.Logger;
import android.medicalcloud.com.yiapp.yilian.configer.contance.Constance;
import android.medicalcloud.com.yiapp.yilian.configer.enums.HttpConfig;
import android.medicalcloud.com.yiapp.yilian.logic.CommonController;
import android.medicalcloud.com.yiapp.yilian.logic.OrderController;
import android.medicalcloud.com.yiapp.yilian.logic.PushController;
import android.medicalcloud.com.yiapp.yilian.logic.UserController;
import android.medicalcloud.com.yiapp.yilian.model.ImConfigModel;
import android.medicalcloud.com.yiapp.yilian.model.OrdersModel;
import android.medicalcloud.com.yiapp.yilian.reiver.MessageReiver;
import android.medicalcloud.com.yiapp.yilian.service.PushIntentService;
import android.medicalcloud.com.yiapp.yilian.ui.ac.LoginActivity;
import android.medicalcloud.com.yiapp.yilian.utils.android.IntentTool;
import android.medicalcloud.com.yiapp.yilian.utils.android.ToastTool;
import android.medicalcloud.com.yiapp.yilian.utils.im.PushUtil;
import android.os.Handler;
import android.os.Message;

import com.igexin.sdk.PushManager;
import com.tencent.TIMCallBack;
import com.tencent.TIMManager;
import com.tencent.openqq.protocol.imsdk.im_common;
import com.tencent.qcloud.presentation.business.LoginBusiness;
import com.tencent.qcloud.presentation.event.MessageEvent;

import java.io.Serializable;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wanggenlin on 17-5-18.
 */
public class LoadActivity extends BaseActivity implements TIMCallBack,ViewNetCallBack{
    MessageReiver reiver;
    private static final String ACTIONNAME_STRING="com.igexin.sdk.action.0Azorryrea8gUk7LwvZJp9";
    @Override
    protected void initView() {
        if (UserController.getInstance().isLogin()){
            UserController.getInstance().loginIm(this);
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        PushManager.getInstance().initialize(this.getApplicationContext(),
                android.medicalcloud.com.yiapp.yilian.service.PushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(),
                PushIntentService.class);

        CommonController.getInstance().config();
        reiver = new MessageReiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTIONNAME_STRING);
        registerReceiver(reiver,intentFilter);

    }

    Handler handler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            Logger.e("-----UserController.getInstance().isLogin()--"+UserController.getInstance().isLogin());
            if (UserController.getInstance().isLogin()){


                return;
            }
            IntentTool.login(LoadActivity.this);
            finish();
        }
    };
    @Override
    public void onSuccess() {
//初始化程序后台后消息推送
        PushUtil.getInstance();
        //初始化消息监听
        MessageEvent.getInstance();
        PushController.getInstance().bind();
        Logger.e("imsdk env " + TIMManager.getInstance().getEnv());
        IntentTool.startMain(this);
        finish();
    }

    @Override
    public void onError(int i, String s) {
        Logger.e("login error : code " + i + " " + s);
        UserController.getInstance().cleanToken();
        switch (i) {
            case 6208:
                //离线状态下被其他终端踢下线
                ToastTool.show("您的账号在其他地方登录，请重新登录");
                break;
            case 6200:
                ToastTool.show("登录失败，当前无网络");
                IntentTool.login(LoadActivity.this);
                finish();
                break;
            default:
                ToastTool.show("IM登录失败，请稍后重试");
                IntentTool.login(LoadActivity.this);
                finish();
                break;
        }
    }
    @Override
    protected int getLayoutId() {
        return R.layout.ac_load;
    }

    @Override
    public void onConnectStart(Object o) {

    }

    @Override
    public void onConnectEnd() {

    }

    @Override
    public void onFail(Exception e, Object o, String reslut) {
        if (UserController.getInstance().isLogin()){
            IntentTool.startMain(LoadActivity.this);
            finish();
        }else{
            IntentTool.login(LoadActivity.this);
            finish();
        }

    }

    @Override
    public void onData(Serializable result, int flag, boolean fromNet, Object o) {

        if (flag== HttpConfig.imSig.getType()){

            if (UserController.getInstance().isLogin()){

                ImConfigModel imModel = (ImConfigModel)result;
                PreferceManager.getInsance().saveValueBYkey("from_identifier",imModel.getFrom_identifier());
                PreferceManager.getInsance().saveValueBYkey("from_usersig",imModel.getFrom_usersig());
                PreferceManager.getInsance().saveValueBYkey("to_identifier",imModel.getTo_identifier());
                PreferceManager.getInsance().saveValueBYkey("to_usersig",imModel.getTo_usersig());
                LoginBusiness.loginIm(PreferceManager.getInsance().getValueBYkey("from_identifier"),
                        PreferceManager.getInsance().getValueBYkey("from_usersig"),
                        Constance.APPID, LoadActivity.this);

                return;
            }
            IntentTool.login(LoadActivity.this);
            finish();


        }
    }
}
